<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <script src="../lib/vue.js"></script>
  <script src="../lib/vue-route.js"></script>
</head>

<body>
  <div id="app">
    <router-link to="/login">登录</router-link>
    <router-link to="/register">注册</router-link>

    <router-view/>
  </div>

  <script>
    
    var login = {
      template: '<h1>登录组件</h1>'
    }
    var register = {
      template: '<h1>注册组件</h1>'
    }

    var router = new VueRouter({
        routes: [{
            path: '/',
            redirect: '/login'
        }, {
          path: '/login',
          // 注意：component的属性值，必须是一个组件模板对象，不能是组件引用名称
          component: login
        }, {
          path: '/register',
          component: register
        }]
    })
    
    // 创建Vue实例，得到ViewModel
    var vm = new Vue({
      el: '#app',
      data: {
        
      },
      methods: {
      },
      router,
      watch: {
           '$route.path': function(newVal, oldVal) {
               console.log(oldVal + "-->" + newVal)
            if(newVal === '/login') {
                console.log("欢迎进入登录页面")
            }

            if(newVal === '/register') {
                console.log("欢迎进入注册页面")
            }
           }
      }
    });
  </script>
</body>

</html>